package com.ib.algokit.forex;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import com.ib.algokit.history.StopLossClosedEvent;

public class StopLossClosedEventChangeHash {
	private TreeMap<Double, StopLossClosedEvent> cache = new TreeMap<Double, StopLossClosedEvent>();
	
	public StopLossClosedEventChangeHash(List<StopLossClosedEvent> stopLossClosedEvents) {
		cache.clear();
		for(StopLossClosedEvent stopLossClosedEvent: stopLossClosedEvents) {
			ForexPairStates forexPairStates=stopLossClosedEvent.getForexPairStates();
			double percChange=forexPairStates.getPercChange();
			if(cache.put(percChange, stopLossClosedEvent)!=null){
				throw new RuntimeException("percChange exists "+percChange);
			}
		}
	}
	
	public List<StopLossClosedEvent> getInterval(Double start, Double end) {        
	    SortedMap<Double, StopLossClosedEvent> subMap = cache.subMap(start, end);
	    List<StopLossClosedEvent> results = new ArrayList<StopLossClosedEvent>();
	    results.addAll(subMap.values());        
	    return results;
	}
}
